package com.tang.subject.infra.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.tang.subject.infra.basic.entity.pojo.SubjectCategory;
import com.tang.subject.infra.basic.mapper.SubjectCategoryDao;
import com.tang.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 题目分类(SubjectCategory)表服务实现类
 *
 * @author makejava
 * @since 2023-12-10 07:35:18
 */
@Slf4j
@Service("subjectCategoryService")
public class SubjectCategoryServiceImpl implements SubjectCategoryService {
    @Resource
    private SubjectCategoryDao subjectCategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SubjectCategory queryById(Long id) {
        return this.subjectCategoryDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(SubjectCategory subjectCategory) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.subjectCategory:{}", JSON.toJSONString(subjectCategory));
        }
        return this.subjectCategoryDao.insert(subjectCategory);
    }

    /**
     * 修改数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public int update(SubjectCategory subjectCategory) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryController.update.subjectCategory:{}", JSON.toJSONString(subjectCategory));
        }
        return subjectCategoryDao.update(subjectCategory);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.subjectCategoryDao.deleteById(id) > 0;
    }

    /**
     * 查询分类数据
     *
     * @param subjectCategory
     * @date 2024/1/26 15:50
     **/
    @Override
    public List<SubjectCategory> queryCategory(SubjectCategory subjectCategory) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryService.queryCategory.subjectCategory:{}", JSON.toJSONString(subjectCategory));
        }
        return subjectCategoryDao.queryCategory(subjectCategory);
    }

    /**
     * 根据分类id查询对应题目数目
     *
     * @param id 分类id
     * @return java.lang.Integer
     * @author Tang
     * @date 2024/2/18 15:04
     **/
    @Override
    public Integer querySubjectCountById(Long id) {
        return subjectCategoryDao.querySubjectCount(Collections.singletonList(id));
    }

    /**
     * 根据分类id查询对应题目数目
     *
     * @param ids 分类id列表
     * @return java.lang.Integer
     * @author Tang
     * @date 2024/2/18 15:04
     **/
    @Override
    public Integer querySubjectCountByIds(List<Long> ids) {
        return subjectCategoryDao.querySubjectCount(ids);
    }

    /**
     * 根据父id查询所有的子id
     *
     * @param categoryId 分类id
     * @return java.util.List<com.tang.subject.infra.basic.entity.pojos.SubjectCategory>
     * @author Tang
     * @date 2024/2/20 13:24
     **/
    @Override
    public List<SubjectCategory> querySonIdsByParentId(Long categoryId) {
        return subjectCategoryDao.querySonIdsByParentId(categoryId);
    }
}
